﻿(function () {
    "use strict";
    function bindWebLinks(element) {
        $('*[data-page-weblink]', element).each(function () {
            var target = $(this).data('page-weblink');

            if (target && target.indexOf('/') < 0) {
                var tmp = MCNEXT.Utils.readProperty(window, target);
                if (tmp) {
                    target = tmp;
                }
            }

            if (target) {
                $(this).tap(function (eltarg) {
                    var actionArgs = $(eltarg).data('page-action-args');
                    if (actionArgs && typeof actionArgs == 'string') {
                        try {
                            var tmp = MCNEXT.Utils.readValue(eltarg, actionArgs);
                            if (tmp) {
                                actionArgs = tmp;
                            }
                            else {
                                actionArgs = JSON.parse(actionArgs);
                            }
                        }
                        catch (exception) {
                            return;
                        }
                    }
                    var uri = new Windows.Foundation.Uri(target);
                    Windows.System.Launcher.launchUriAsync(uri);
                });
            }
        });
    }

    WinJS.UI.Pages.define("/pages/home/home.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.

        prepareData: function (element, options) {

        },

        prepare: function (element, options) {
            bindWebLinks(element);
            var prom = [];
            var mcncontent = element.querySelectorAll('.mcn-hub-section-content');
            var sectionCore = element.querySelectorAll('#sectionCore');

            for (var i = 0; i < mcncontent.length; i++) {
                prom.push(WinJS.UI.process(mcncontent[i]));
            }
            prom.push(WinJS.UI.process(sectionCore));
            return new WinJS.Promise.join(prom);
        },
        ready: function (element, options) {
            var pageHub = element.querySelector('#pageHub');
            setImmediate(function () {
                pageHub.winControl.selectedIndex = 0;
            });



            // TODO: Initialize the page here.
        }
    });
})();
